﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sysgeprava.BL;
using Sysgeprava.Entities;
using Microsoft.Practices.Unity;


namespace Sysgeprava.Web.Controllers.Mantenimiento
{
    public class RazaController : Controller
    {
        [Dependency]
        public IRazaBL razaBL { get; set; }
        //
        // GET: /Raza/

        public ActionResult Index()
        {
            var lista = razaBL.GetAllFromRaza();
            return View(lista);
        }

        [HttpPost]
        public ActionResult Index(string criterio = "")
        {
            var lista = razaBL.GetAllFromRazaByCriterio(criterio);
            return View(lista);
        }

        public ActionResult Create()
        {
            var raza = new RAZA();
            return View(raza);
        }

        [HttpPost]
        public ActionResult Create(RAZA _raza)
        {
            var raza = new RAZA();

            try
            {
                raza.Descripcion = _raza.Descripcion.ToUpper();
                razaBL.AddRaza(raza);

                //TempData["AlertMessage"] = "Registro Correcto";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg = "Raza ya Registrada";
                TempData["AlertMessage"] = msg;
                return RedirectToAction("Create");
            }
        }

        public ActionResult Edit(Int32 id)
        {
            var raza = razaBL.GetAllFRomRazaById(id);
            return View(raza);
        }

        [HttpPost]
        public ActionResult Edit(RAZA _raza)
        {
            var raza = new RAZA();
            try
            {
                raza.Id = _raza.Id;
                raza.Descripcion = _raza.Descripcion.ToUpper();
                razaBL.UpdateRaza(raza);

                //TempData["AlertMessage"] = "Edición Correcta";
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                msg = "Raza ya Registrada";
                TempData["AlertMessage"] = msg;
                return RedirectToAction("Edit");
            }
        }

        public ActionResult Delete(Int32 id)
        {
            razaBL.DeleteRaza(id);
            return RedirectToAction("Index");
        }

    }
}
